package com.banch.cloud.seata.account.controller;

import com.banch.cloud.seata.account.domain.CommonResult;
import com.banch.cloud.seata.account.service.AccountService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
public class AccountController {

    @Resource
    AccountService accountService;

    /**
     * 扣减账户余额
     */
    @RequestMapping("/account/decrease")
    public CommonResult decrease(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money) {
        try {
            accountService.decrease(userId, money);
        } catch (Exception e) {
            e.printStackTrace();
            // 注意：如果捕获了异常，一定要抛出给服务调用者，这样调用者才会发起回滚分布式事务
            throw e;
        }
        return new CommonResult(200, "扣减账户余额成功！");
    }

}